package com.sinosoft.dsp.component.bpm.viewbean;

import java.util.ArrayList;
import java.util.List;

import org.jbpm.api.task.Task;

import com.sinosoft.dsp.component.bpm.facade.engine.ProcessTaskFacade;
import com.sinosoft.dsp.component.bpm.util.JbpmTaskStateType;
import com.sinosoft.dsp.platform.components.system.IModuleBean;
import com.sinosoft.dsp.platform.components.system.ModuleBean;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.security.services.SinoAuthenticator;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.entity.ISecurityUser;
import com.sinosoft.dsp.system.utils.DateUtils;

public class BpmHomeDealViewBean extends ModuleBean implements IModuleBean {

	private static final long serialVersionUID = -2212704833341425905L;

	private List<String[]> assigen = new ArrayList<String[]>();

	private List<String[]> unassigen = new ArrayList<String[]>();

	@Override
	public void construct(String sceneId, String layout) throws SinoException {
		super.construct(sceneId, layout);
		reload();
	}

	@Override
	public void handleFlush() {
		reload();
	}

	public void reload() {
		assigen.clear();
		unassigen.clear();

		ProcessTaskFacade taskFacade = SinoManagedBean
				.findBean("processTaskFacade");
		SinoAuthenticator sa = SinoManagedBean.findBean("sinoAuthenticator");
		ISecurityUser user = sa.getCurrentUser();
		if (user != null) {
			List<Task> tasks = taskFacade.getAssignedTaskSetByUserId(user
					.getUseruuid());

			if (tasks != null && tasks.size() > 0) {
				int num = (tasks.size() < 10) ? tasks.size() : 10;
				Task t;
				for (int i = 0; i < num; i++) {
					t = tasks.get(i);
					assigen.add(createRowData(t, JbpmTaskStateType.ASSIGNED
							.getTitle()));
				}
			}

			tasks = taskFacade.getUnassignedTaskSetByUserId(user.getUseruuid());

			if (tasks != null && tasks.size() > 0) {
				int num = (tasks.size() < 10) ? tasks.size() : 10;
				Task t;
				for (int i = 0; i < num; i++) {
					t = tasks.get(i);
					unassigen.add(createRowData(t, JbpmTaskStateType.UNASSIGNED
							.getTitle()));
				}
			}
		}

	}

	private String[] createRowData(Task t, String typeTitle) {
		String[] d = new String[5];
		d[0] = t.getId();
		d[1] = t.getActivityName();
		d[2] = typeTitle;
		d[3] = DateUtils.dat2str(t.getCreateTime());
		d[4] = t.getExecutionId();
		return d;
	}

	public List<String[]> getAssigen() {
		return assigen;
	}

	public void setAssigen(List<String[]> assigen) {
		this.assigen = assigen;
	}

	public List<String[]> getUnassigen() {
		return unassigen;
	}

	public void setUnassigen(List<String[]> unassigen) {
		this.unassigen = unassigen;
	}

}
